package sdk.webview.fmc.com.fmcsdk.check.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import sdk.webview.fmc.com.fmcsdk.R;

public class WorkResultAdapter extends RecyclerView.Adapter<WorkResultAdapter.ViewHolder> {

    private Context mContext;
    private List<String> list;

    private OnItemClickListener listener;

    public WorkResultAdapter(Context context, List<String> list) {
        this.mContext = context;
        this.list = list;
    }

    @NonNull
    @Override
    public WorkResultAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new WorkResultAdapter.ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_pause_work_result, null));
    }

    @Override
    public void onBindViewHolder(@NonNull WorkResultAdapter.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {

        final String item = list.get(position);
        holder.tv_result.setText(item);
        holder.ll_item.setOnClickListener(v -> {
            if(listener!=null){
                listener.onItemClick(position,item);
            }

        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_result;
        private LinearLayout ll_item;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_result = itemView.findViewById(R.id.tv_result);
            ll_item = itemView.findViewById(R.id.ll_item);
        }
    }

    // 定义点击事件接口
    public interface OnItemClickListener {
        void onItemClick(int position, String item);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
